<template>
  <header class="navbar px-24px" v-acrylic>
    <!-- <x-input-search class="w60! mr-4" placeholder="developmenting……" /> -->

    <x-switch v-model:value="isDark">
      <template #checked><x-icon type="moon" size="20px" /></template>
      <template #unchecked><x-icon type="sun" size="20px" /></template>
    </x-switch>

    <a class="color-inherit" href="https://gitee.com/httpsgiteecomepalserver/x-ui" target="_blank">
      <x-icon type="github" size="28px" />
    </a>
  </header>

  <div class="subnav" v-acrylic>
    <div class="flex items-center px-24px text-16px" @click="toggleOpen()">
      <x-icon type="windows" size="20"></x-icon>
      <span class="ml-2">Menu</span>
    </div>
  </div>

  <div v-if="menuOpend" class="fixed inset-0 bg-black opacity-60 z-1" @click="menuOpend = false"></div>

  <!-- 菜单 -->
  <aside :class="['sidebar', menuOpend && 'open']">
    <x-menu v-model:value="selected" class="menu" :options="menus" @select="menuOpend = false"></x-menu>
  </aside>

  <!-- 主要内容 -->
  <main class="page-container">
    <router-view />
  </main>
</template>

<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import { menus } from './menus'
import { isDark } from '../utils/useTheme'
import { computedEager, useWindowSize, useToggle } from '@vueuse/core'
import { useRouter } from 'vue-router'
import { useMemo } from '../../components/_util/hooks'

const { width } = useWindowSize()
const lt960 = computedEager(() => width.value < 960)

const menuOpend = ref(false)
const toggleOpen = useToggle(menuOpend)

watchEffect(() => {
  if (lt960.value) return
  menuOpend.value = false
})

const currentRoute = useRouter().currentRoute
const selected = useMemo(() => currentRoute.value.name as string)
</script>

<style>
:root {
  --header-height: 55px;
  --sidebar-width: 220px;
}
</style>

<style lang="scss">
.navbar {
  position: sticky !important;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: var(--header-height);
  will-change: backdrop-filter;
  z-index: 1;
  @media screen and (min-width: 960px) {
    top: 0;
  }
  > * {
    margin-left: 20px;
  }
}

.subnav {
  position: sticky;
  top: 0;
  display: flex;
  height: 50px;
  will-change: backdrop-filter;
  z-index: 1;
  @media screen and (min-width: 960px) {
    display: none;
  }
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  transform: translateX(-100%);
  transition: transform 0.5s cubic-bezier(0.23, 0.85, 0.13, 1);
  z-index: 1;
  @media screen and (min-width: 960px) {
    top: var(--header-height);
    transform: none;
  }
  &.open {
    transform: none;
  }
  > .menu {
    width: 100%;
    height: 100%;
    padding-bottom: 22px;
    overflow: hidden auto;
  }
}
.page-container {
  padding: 32px 24px;
  @media screen and (min-width: 960px) {
    margin-left: var(--sidebar-width);
    padding: 32px;
  }
}
</style>
